var TagMail = {

    subject : null,
    sender : null,
    tagMenu : null,
    seperator : null,

    onLoad : function(event) {

        document.getElementById('threadPaneContext')
            .addEventListener("popupshowing", function(e) { TagMail.showContextMenu(e); }, false);
	//get curent sender address | just for test | future will be the URI of mail to do some operations.
	//Every time one mail is selected some info attached with that mail will be retrieved.
        document.getElementById('threadTree')
            .addEventListener("select", function(e) { TagMail.showContextMenu(e); }, false);

        this.tagMenu    = document.getElementById("tagmail");

        this.seperator      = document.getElementById("nepomuktager-sep");

    },

    showContextMenu : function(event) {
        var msgURI     //selected message uri
          , msgHdr     //selected message header
          , parser     //to parse author
          , hide
          , senderAddress
	  , mailURI;


        hide = GetNumSelectedMessages() != 1;

        this.tagMenu.hidden = hide;
        this.seperator.hidden = hide;

        if (hide)
            return;

        msgURI = GetLoadedMessage();
        if (!msgURI)
            return;

        msgHdr = messenger.messageServiceFromURI(msgURI).messageURIToMsgHdr(msgURI);

        parser = Components.classes["@mozilla.org/messenger/headerparser;1"]
                    .getService(Components.interfaces.nsIMsgHeaderParser);

        senderAddress = {};
        parser.parseHeadersWithArray(msgHdr.author, senderAddress, {}, {});
        this.sender = senderAddress.value;

        this.subject = msgHdr.mime2DecodedSubject.toString();

	this.mailURI = msgURI.toString();

	document.getElementById("authorID").setAttribute("value",this.sender);

    },

    showSubject: function(event) {
        alert(this.subject);
    },

    showSender: function(event) {
        alert(this.sender);
    }

};

window.addEventListener("load", function(event) { TagMail.onLoad(event); }, false);
